package com.loves.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loves.admin.model.User;
import com.loves.admin.service.ExcelService;
import com.loves.admin.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.FileNotFoundException;

@RestController
@RequestMapping("/user")
@Api(tags = "用户接口")
public class UserController {

    private Logger logger = LoggerFactory.getLogger(UserController.class);

    @Resource
    public UserService userService;

    @Resource
    public ExcelService excelService;

    @ApiOperation(value = "用户分页查询",notes = "根据年龄查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNo",value = "页数",required = true),
            @ApiImplicitParam(name = "pageSize",value = "条数",required = true),
            @ApiImplicitParam(name = "age",value = "页数",required = true)
    })
    @PostMapping("/selectByAll")
    public IPage<User> selectByAll(HttpServletRequest request, @RequestParam int age){
        Integer pageNo = Integer.valueOf(request.getParameter("pageNo"));
        Integer pageSize = Integer.valueOf(request.getParameter("pageSize"));
        Page<User> page = new Page<>(pageNo, pageSize);
        return userService.selectPageVo(page,age);
    }

    @ApiOperation(value = "用户新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "用户姓名",required = true,dataType = "String"),
            @ApiImplicitParam(name = "age",value = "用户年龄",required = true,dataType = "int"),
            @ApiImplicitParam(name = "gender",value = "用户性别",required = true,dataType = "int")
    })
    @PostMapping("/insert")
    public String insert(@RequestBody User user){
        if(userService.insert(user)){
            return "新增成功！";
        }else {
            return "新增失败！";
        }
    }

    @ApiOperation(value = "用户修改")
    @PostMapping("/update")
    public String update(@RequestBody User user){
        logger.info("user:",user);
        if(userService.update(user)){
            return "修改成功！";
        }else {
            return "修改失败！";
        }
    }

    @ApiOperation(value = "用户删除")
    @ApiImplicitParam(name = "id",value = "id",dataType = "String",required = true)
    @DeleteMapping("/delete")
    public String delete(@RequestParam String id){
        if(userService.delete(id)){
            return "删除成功！";
        }else {
            return "删除失败！";
        }
    }

    @PostMapping("/exportUser")
    public void exportUser(){
        excelService.exportUser();
    }

    @PostMapping("/dynamicExport")
    public void dynamicExport() throws FileNotFoundException {
        excelService.dynamicExport();
    }

}
